Search this book | Previous | Table of contents | Next

Making your scripts HTTP-friendly


This section describes how to make your scripts conform to the HTTP and HTML standards.

There are two major problems with hello-world-01.script. First, it is not HTTP-friendly because it does not return the standard HTTP header telling the client application the origin of its input and type of document being returned. This is a responsibility of any script you write for your WWW server.

Second, the output of hello-world-01.script is an invalid HTML document; when writing scripts for your WWW server, you must format the results into valid HTML. (At the present time, many client applications will accept the raw output from hello-world-01.script, but future clients may not be so forgiving).

Consequently, the following script (hello-world-02.script), while it functions exactly the same as the previous script, is a nicer "network citizen" because it returns an HTTP header and valid HTML:

-- define the standard HTTP header
set LF to ASCII character (10)
set CR to return
set CRLF to CR & LF
set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
	"Server: MacHTTP" & CRLF & ¬
	"MIME-Version: 1.0" & CRLF & ¬
	"Content-type: text/html" & CRLF & CRLF

-- return the results as an HTML file
return http_10_header & ¬
	"<html>" & ¬
	"<head>" & ¬
	"<title>Hello, World</title>" & ¬
	"</head>" & ¬
	"<body>Hello, World!</body" & ¬
	"</html>"

Use the same method to create this script as the first script:

  1. Launch your copy of Script Editor (or you could use any text editor)
  2. Create a new file
  3. Enter the single line above
  4. Save it as a text file within your server's directory structure making sure you give it a file name with a .script extension
The first part of this script defines a standard HTTP header as defined by the HTTP protocol. To do so, it first creates variables for the carriage return/line feed sequence. It then defines a variable (http_10_header). This variable will describe to the client application the version of HTTP being implemented, the version of MIME being implemented, and the MIME type of information to follow. When writing CGI scripts for MacHTTP, your http_10_header will be defined like this. If you are writing scripts for WebSTAR, then you will want to replace the string "MacHTTP" with "WebSTAR".

The second part of the script returns text to the client. This text first includes the header described above and then the shortest of HTML documents marking up the "Hello, World" text.

Obviously, this script does not do very much. By adding a few lines of code and a bit of tweaking, you can make the script return the current date and time:

-- define the standard HTTP header
set LF to ASCII character (10)
set CR to return
set CRLF to CR & LF
set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
	"Server: MacHTTP" & CRLF & ¬
	"MIME-Version: 1.0" & CRLF & ¬
	"Content-type: text/html" & CRLF & CRLF

-- get the date and time
set theDateAndTime to the (current date) as string

-- return the results as an HTML file
return http_10_header & ¬
	"<html>" & ¬
	"<head>" & ¬
	"<title>Hello, Reader</title>" & ¬
	"</head>" & ¬
	"<body>The date and time is " & theDateAndTime & "." & ¬
	"</body" & ¬
	"</html>"
To summarize, this scripts demonstrates how to create the standard HTTP header and how to send the header and marked up HTML document back to the client application. In performing these functions, your CGI scripts conform to standards.


Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.